package priv.books.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import priv.books.mapper.BooksMapper;
import priv.books.mapper.BooksTypeMapper;
import priv.books.pojo.Books;
import priv.books.pojo.BooksType;
@Service
public class BooksTypeService extends ServiceImpl<BooksTypeMapper, BooksType> {

    @Autowired
    private BooksTypeMapper booksTypeMapper;

    @Autowired
    private BooksMapper booksMapper;

    /**
     * 分页查询数据
     * @param booksType
     * @return
     */
    public List<BooksType> getAllInfo(BooksType booksType) {
        QueryWrapper<BooksType> queryWrapper = new QueryWrapper<>();
        if(booksType != null) {
            queryWrapper.like(!StrUtil.isBlankOrUndefined(booksType.getTypeName()), "type_name", booksType.getTypeName());
        }
        return booksTypeMapper.selectList(queryWrapper);
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    public Integer deleteInfo(Integer id) {
        // 删除前首先查看该类型下是否还有绑定的书籍信息
        List<Books> books = booksMapper.selectList(new QueryWrapper<Books>().eq("type_id", id));
        if(books != null && books.size() > 0) {
            return -500;
        }
        // 无绑定书籍信息可以删除
        return booksTypeMapper.delete(new QueryWrapper<BooksType>().eq("id", id));
    }

    /**
     * 批量删除信息
     * @param ids
     * @return
     */
    public Integer deleteInfo(String ids) {
        List<String> split = StrUtil.split(ids, ',', true, true);
        // 删除前首先查看该类型下是否还有绑定的书籍信息
        List<Books> books = booksMapper.selectBatchIds(split);
        if(books != null && books.size() > 0) {
            return -500;
        }
        // 无绑定书籍信息可以删除
        return booksTypeMapper.deleteBatchIds(split);
    }
}
